package edu.nuist.studentadmin.servlet;

import edu.nuist.studentadmin.dao.impl.StudentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkLogin")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        //1.获取用户的输入
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2. 验证用户名密码是否正确
        boolean b = new StudentDaoImpl().checkLogin(username, password);
        //3. 根据验证结果返回不同的页面
        if(b){
            request.getRequestDispatcher("/students.jsp").forward(request,response);
        } else{
           response.getWriter().write("<script>alert('用户名或者密码错误');location.href='login.jsp'</script>");
        }

    }
}
